package com.weng.cloud.commons.base;

import org.apache.commons.lang3.ObjectUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author: weng
 * @Date: 2022/6/12
 * @Description: com.weng.cloud.commons
 */

public class TimeUtil {

    private TimeUtil() {
    }

    private static final String PATTERN_YMD_UNDERLINE;

    private static final String PATTERN_YM_UNDERLINE;

    private static final String PATTERN_DEFAULT;

    private static final DateFormat DATE_FORMAT_DEFAULT;

    static {
        PATTERN_YMD_UNDERLINE = "yyyy-MM-dd";
        PATTERN_YM_UNDERLINE = "yyyy-MM";
        PATTERN_DEFAULT = PATTERN_YMD_UNDERLINE;

        DATE_FORMAT_DEFAULT = new SimpleDateFormat(PATTERN_DEFAULT);
    }

    private static DateFormat dateFormat(DateFormat dateFormat) {
        return ObjectUtils.isEmpty(dateFormat) ? DATE_FORMAT_DEFAULT : dateFormat;
    }

    public static String toText(Date date, DateFormat dateFormat) {
        return dateFormat(dateFormat).format(date);
    }

    public static String toText(Date date) {
        return toText(date, null);
    }

    public static String toText(Calendar calendar) throws ParseException {
        return toText(toDate(calendar));
    }

    public static Date toDate(String text, DateFormat dateFormat) throws ParseException {
        return dateFormat(dateFormat).parse(text);
    }

    public static Date toDate(String text) throws ParseException {
        return toDate(text, null);
    }

    public static Date toDate(Calendar calendar) throws ParseException {
        return calendar.getTime();
    }

    public static Calendar toCalendar(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar toCalendar(String text) throws ParseException {
        return toCalendar(toDate(text));
    }

    public static Calendar move(Calendar calendar, int field, int amount) {
/*
        Calendar calendarReturned = ObjectUtil.deepClone(calendar, Calendar.class);
        calendarReturned.add(field, amount);
        return calendarReturned;
*/
        calendar.add(field, amount);
        return calendar;
    }

/*
    public static Calendar moveDay(Calendar calendar, int amount) {
        return move(calendar, Calendar.DATE, amount);
    }

    public static Calendar moveMonth(Calendar calendar, int amount) {
        return move(calendar, Calendar.MONTH, amount);
    }

    public static Calendar moveYear(Calendar calendar, int amount) {
        return move(calendar, Calendar.YEAR, amount);
    }

    public static Date move(Date date, int field, int amount) throws ParseException {
        return toDate(move(toCalendar(date), field, amount));
    }

    public static Date moveDay(Date date, int amount) throws ParseException {
        return toDate(move(toCalendar(date), Calendar.DATE, amount));
    }
*/

    public static Date moveMonth(Date date, int amount) throws ParseException {
        return toDate(move(toCalendar(date), Calendar.MONTH, amount));
    }

/*
    public static Date moveYear(Date date, int amount) throws ParseException {
        return toDate(move(toCalendar(date), Calendar.YEAR, amount));
    }

    public static String move(String text, int field, int amount) throws ParseException {
        return toText(move(toCalendar(text), field, amount));
    }

    public static String moveDay(String text, int amount) throws ParseException {
        return toText(move(toCalendar(text), Calendar.DATE, amount));
    }

    public static String moveMonth(String text, int amount) throws ParseException {
        return toText(move(toCalendar(text), Calendar.MONTH, amount));
    }

    public static String moveYear(String text, int amount) throws ParseException {
        return toText(move(toCalendar(text), Calendar.YEAR, amount));
    }
*/

    public static Date nextMonth(Date date) throws ParseException {
        return moveMonth(date, 1);
    }

    public static int year(Date date){
        return toCalendar(date).get(Calendar.YEAR);
    }

    public static int month(Date date){
        return toCalendar(date).get(Calendar.MONTH) + 1;
    }
}
